//
//  DHCart_WashingItemCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/14.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHCart_WashingItemCell.h"

@interface DHCart_WashingItemCell()

@property (strong, nonatomic) NSDictionary *dic;

@end

@implementation DHCart_WashingItemCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.selectedButton bk_addEventHandler:^(id sender) {
        if (self.itemSelected) {
            [self.selectedButton setImage:[UIImage imageNamed:@"cart_unselected.png"] forState:UIControlStateNormal];
            self.itemSelected = NO;
        }else{
            [self.selectedButton setImage:[UIImage imageNamed:@"cart_selected.png"] forState:UIControlStateNormal];
            self.itemSelected = YES;
        }
        [self routerEventWithName:@"washing_selected" userInfo:@{@"item":self.dic,@"selected":@(self.itemSelected)}];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (IBAction)jianAction:(id)sender {
    NSInteger count = [self.countLabel.text integerValue] - 1;
    if (count <= 0) {
        [UIAlertView bk_showAlertViewWithTitle:@"提示" message:@"是否从购物车中删除该商品?" cancelButtonTitle:@"否" otherButtonTitles:@[@"是"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) {
            if (buttonIndex == 1) {
                [self routerEventWithName:@"washing_delete" userInfo:@{@"item":self.dic}];
            }
        }];
    }else{
        [[DHNetAPIClient shareDHNetAPIClient] request_user_cart_modify_countWithParams:@{@"cart_id":[self.dic objectForKey:@"cart_id"],@"count":@(count)} andBlock:^(id data, NSError *error) {
            [self routerEventWithName:@"change_count" userInfo:nil];
        }];
    }
}

- (IBAction)jiaAction:(id)sender {
    NSInteger count = [self.countLabel.text integerValue] + 1;
    [[DHNetAPIClient shareDHNetAPIClient] request_user_cart_modify_countWithParams:@{@"cart_id":[self.dic objectForKey:@"cart_id"],@"count":@(count)} andBlock:^(id data, NSError *error) {
        [self routerEventWithName:@"change_count" userInfo:nil];
    }];
}

- (void)updateCell:(NSDictionary *)dic{
    self.dic = dic;
    [self.imgView dh_setImageURLString:[dic objectForKey:@"image"]];
    self.nameLabel.text = [dic objectForKey:@"name"];
    self.leftPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"wash_price"] floatValue]];
    
    NSInteger count = [[dic objectForKey:@"count"] integerValue];
    self.rightPriceLabel.text = [NSString stringWithFormat:@"￥%.2f",[[dic objectForKey:@"wash_price"] floatValue] * count];
    
    self.countLabel.text = [NSString stringWithFormat:@"%ld",(long)count];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
